<?php
// +----------------------------------------------------------------------
// | 功能说明"
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// | jmzhfsauto
// +----------------------------------------------------------------------
namespace app\admin\model;
use Think\Db;
use think\Model;

class TracklogModel extends Model
{
    
    protected $name = 'track_log';
    protected $autoWriteTimestamp = true;//时间自动更新 create_time ,update_time

   protected $field = true;
    public $title = '名称';


    /**
     * 获取跟进日志 --进展示意图
     */
    public function get_log($d_id,$data){
        $where=array(
            'isdelete'=>0,
            'relation_id'=>$d_id
        );
        $list = $this->where($where)->order(['level'=>'asc'])->field('id,level,create_time')->select();
        foreach ($list as $k=>$v){
            $a = $v->toarray();
            if(!empty($a)){
                $a['title'] = '第'.$a['level'].'次跟进';
                array_push($data,$a);
            }
        }
        $res['data'] = $data;
        $list = (array)$list;
        if(!empty($list)){
            $res['count'] = count($list)+1;
        }
        return $res;
    }

    /**
     * 获取跟进日志--时间轴
     */
    public function get_time_line($d_id){
        $where=array(
            'a.isdelete'=>0,
            'a.relation_id'=>$d_id
        );
        $list = $this->alias('a')
            ->join('sys_user u', 'a.user_id = u.id', 'left')
            ->where($where)->order(['create_time'=>'asc'])
            ->field('a.id,a.create_time,a.level,u.name market_user,a.create_time,a.speak')
            ->select();
            foreach ($list as $k=>$v){
                $a = $v->toarray();
                if(!empty($a)){
                    $a['title'] = '第'.$a['level'].'次跟进';
                    $list[$k] = $a;
                }
            }
        return $list;
    }

   
    public function edit($param)
    {
        try{
          

            

            $result =  $this->update($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('tracklog/index'), '修改操作日志成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


    public function add($param)
    {
        try{
            $param['isdelete']=0;
            $result =  $this->save($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                    return msg(1,url('tracklog/index'), '添加跟进信息成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    public function getById($id)
    {
        return $this->where('id', $id)->find();
    }
    
    public function delById($id)
    {
        try{
            $this->where('id', $id)->update(['isdelete'=>'1']);
            return msg(1, '', '删除成功');
        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

  

    public function getByWhere($where, $offset, $limit,$whereor='')
    {
        return $this->where($where)->where($whereor)->limit($offset, $limit)->order('id desc')->select();
    }

    public function getAllCount($where,$whereor='')
    {
        return $this->where($where)->where($whereor)->count();
    }

    //获取当前模型数据表
    public function getTable(){
        return $this->name;
    }
   /**/

}
